import type { Router } from 'vue-router';
import { useMultipleTabStore } from '@/store/modules/multipleTab';
import { PageEnum } from '@/enums/pageEnum';
import { removeTabChangeListener } from '@/logics/mitt/routeChange';
import { AppStorage, PermissionStorage, UserStorage } from '@2/app/core/ioc';

export function createStateGuard(router: Router) {
  router.afterEach((to) => {
    // Just enter the login page and clear the authentication information
    if (to.path === PageEnum.BASE_LOGIN) {
      const tabStore = useMultipleTabStore();
      // TODO-BaiHoo-Update: 更新为新的 store 使用方式
      /*const userStore = useUserStore();
      const appStore = useAppStore();
      const permissionStore = usePermissionStore();*/
      AppStorage.destroy();
      PermissionStorage.destroy();
      tabStore.resetState();
      UserStorage.destroy();
      removeTabChangeListener();
    }
  });
}
